# @Time    :2025/4/13 11:53
"""
队列，先进先出，从队尾进 ，队头出
"""


class Node:
    def __init__(self, data):
        self.data = data #存储数据
        self.next = None


class Queue:
    def __init__(self):
        self.__size = 0
        self.__head = None
        self.__tail = None

    @property
    def size(self):
        return self.__size

    def is_empty(self):
        return self.__size == 0

    def enqueue(self, data):
        """入队 从队尾进"""
        new_Node = Node(data)#使用新传入的data创建一个新的节点对象new_Node
        if self.__size == 0: #如果没有元素 就是队列为空，就将头节点和尾节点都指向新节点
            self.__head = new_Node
            self.__tail = new_Node
        else:
            self.__tail.next = new_Node#将当前节点的尾节点的下一个节点指向新的节点
            self.__tail = new_Node #更新 新的节点为尾节点
        self.__size += 1

    def dequeue(self):
        """出队，从头出"""
        if self.is_empty():
            raise Exception("队列为空")
        data = self.__head.data
        self.__head = self.__head.next
        self.__size -= 1
        return data

    def peek(self):
        """访问队首"""
        if self.is_empty():
            raise Exception("队列为空")
        else:
            return self.__head.data


q = Queue()
q.enqueue(19)
q.enqueue(23)
q.enqueue(1)
q.enqueue(3)

print(q.size)
print(q.is_empty())
print(q.dequeue())  # 先进先出
print(q.peek())
